package com.koron.css2.meterService.impl;

import com.koron.css2.baseConfig.bean.MeterBoreBean;
import com.koron.css2.baseConfig.bean.MeterFactoryBean;
import com.koron.css2.baseConfig.bean.MeterModelBean;
import com.koron.css2.baseConfig.mapper.MeterBoreMapper;
import com.koron.css2.baseConfig.mapper.MeterFactoryMapper;
import com.koron.css2.baseConfig.mapper.MeterModelMapper;
import com.koron.css2.common.dto.AttachmentDto;
import com.koron.css2.common.service.AbstractExportExcel;
import com.koron.css2.meterService.bean.MsInfoChangeDetailBean;
import com.koron.css2.meterService.bean.query.MsInfoChangeQueryBean;
import com.koron.css2.meterService.dto.MsInfoChangeDto;
import com.koron.css2.meterService.dto.MsInfoDto;
import com.koron.css2.meterService.mapper.MsInfoChangeDetailMapper;
import com.koron.css2.meterService.mapper.MsInfoChangeMapper;
import com.koron.css2.meterService.mapper.MsInfoTempMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.CommonUtils;
import com.koron.css2.serviceManage.utils.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


/**
 * 变更单据导出
 * @Author 张涛
 */
public class WmChgExport extends AbstractExportExcel {

    @Override
    public AttachmentDto getAttachment(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        MsInfoChangeQueryBean bean = JsonUtils.objectToPojo(req.getData(), MsInfoChangeQueryBean.class);
        MsInfoChangeMapper mapper = factory.getMapper(MsInfoChangeMapper.class);
        MsInfoTempMapper msInfoMapper = factory.getMapper(MsInfoTempMapper.class);
        MsInfoChangeDetailMapper detailMapper = factory.getMapper(MsInfoChangeDetailMapper.class);
        List<MsInfoChangeDto> list = mapper.selectList(bean);
        //有序map，规定导出顺序
        List<Map<String,Object>> result = new ArrayList<Map<String,Object>>();
        Map<String, Object> titalMap = tital();
        result.add(titalMap);//标题放在第一行
        for (MsInfoChangeDto msInfoChangeDto:list) {
            List<MsInfoChangeDetailBean> details = detailMapper.selectList(msInfoChangeDto.getId());
            for (MsInfoChangeDetailBean detail : details) {
                Map<String,Object> map = new LinkedHashMap<String, Object>();
                map.put("billNo",msInfoChangeDto.getBillNo());
                map.put("billDate",msInfoChangeDto.getBillDate());
                map.put("comments",msInfoChangeDto.getComments());
                map.put("createName",msInfoChangeDto.getCreateName());
                MsInfoDto msInfo = msInfoMapper.selectById(detail.getMsInfoId());
                msInfo.setOperateType(detail.getOperateType());
                setMsInfo(factory, msInfo, map);
                result.add(map);
            }

        }

        // 返回结果
        AttachmentDto attachment = new AttachmentDto();
        attachment.setBtlTemplate("WmChgExport.btl");
        attachment.setFileTemplate("WmChgExport.xlsx");
        attachment.setData(result);
        attachment.setFileType("excel");
        attachment.setFilename("变更单据导出" + CommonUtils.getCurrentDate());
        return attachment;
    }


    public MsInfoDto setMsInfo(SessionFactory factory, MsInfoDto t,Map<String,Object> map) {
        MeterBoreMapper meterBoreMapper = factory.getMapper(MeterBoreMapper.class);
        // 水表口径
        if (StringUtils.isNotBlank(t.getMeterBore())){
            MeterBoreBean meterBoreBean = meterBoreMapper.selectMeterBoreById(t.getMeterBore());
            if (meterBoreBean !=null){
                t.setMeterBoreName(meterBoreBean.getBoreName());
            }
        }
        //水表厂商
        MeterFactoryMapper meterFactoryMapper=factory.getMapper(MeterFactoryMapper.class);
        MeterFactoryBean factoryBean =  meterFactoryMapper.selectMeterFactoryById(t.getMeterFactory());
        // 水表厂家
        t.setMeterFactoryName(factoryBean==null?t.getMeterFactory():factoryBean.getFactoryName());
        //水表型号
        MeterModelMapper mapper = factory.getMapper(MeterModelMapper.class);
        MeterModelBean modelBean = mapper.selectMeterModelById(t.getMeterModel());
        t.setMeterModelName(modelBean ==null?t.getMeterModel():modelBean.getModelName());
        map.put("userNo",t.getUserNo());
        map.put("meter",t.getMeterNo());
        map.put("factoryNo",t.getFactoryNo());
        map.put("meterBoreName",t.getMeterBoreName());
        map.put("meterFactoryName",t.getMeterFactoryName());
        map.put("meterModelName",t.getMeterModelName());
        map.put("sim",t.getSim());
        map.put("imei",t.getImei());
        map.put("imsi",t.getImsi());
        return t;
    }

    //组装标题
    public Map<String,Object> tital(){
        Map<String,Object> map = new LinkedHashMap<String, Object>();
        map.put("billNo","单据编号");
        map.put("billDate","单据日期");
        map.put("comments","备注");
        map.put("createName","制单人");
        map.put("userNo","用户编号");
        map.put("meter","水表编号");
        map.put("factoryNo","表身码");
        map.put("meterBoreName","水表口径");
        map.put("meterFactoryName","水表厂家");
        map.put("meterModelName","水表型号");
        map.put("sim","sim卡");
        map.put("imei","imei号");
        map.put("imsi","imsi号");
        return map;
    }
}
